package com.file.system.entity.statistics;


import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.List;


/**
 * 首页统计
 *
 * @author jch
 */
@Data
@Accessors(chain = true)
public class HomeStatistics implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 总数
     */
    private Long total;
    /**
     * 已发布数量
     */
    private Long releaseTotal;
    /**
     * 已发布百分比
     */
    private String releaseTotalPercentage;
    /**
     * 待审核数量
     */
    private Long checkTotal;
    /**
     * 待审核百分比
     */
    private String checkTotalPercentage;
    /**
     * 草稿数量
     */
    private Long draftTotal;
    /**
     * 草稿百分比
     */
    private String draftTotalPercentage;
    /**
     * 下线数量
     */
    private Long offlineTotal;
    /**
     * 下线百分比
     */
    private String offlineTotalPercentage;
    /**
     * 退回数量
     */
    private Long backTotal;
    /**
     * 退回百分比
     */
    private String backTotalPercentage;
    /**
     * 每月发布数量
     */
    private List<MonthCreate> monthlyCounts;


    /**
     * 每月创建
     */
    @Data
    public class MonthCreate {
        /**
         * 月份名称
         */
        private String name;
        /**
         * 每月创建数量
         */
        private long createTotal;
    }

    public static HomeStatistics.MonthCreate init(String name, long createTotal) {
        HomeStatistics statistics = new HomeStatistics();
        HomeStatistics.MonthCreate monthCreate = statistics.new MonthCreate();

        monthCreate.setName(name);
        monthCreate.setCreateTotal(createTotal);

        return monthCreate;
    }

    public static HomeStatistics init(Long total, Long releaseTotal, Long checkTotal, Long draftTotal, Long offlineTotal, Long backTotal) {
        HomeStatistics statistics = new HomeStatistics();
        statistics.setTotal(total);
        statistics.setReleaseTotal(releaseTotal);
        statistics.setCheckTotal(checkTotal);
        statistics.setDraftTotal(draftTotal);
        statistics.setOfflineTotal(offlineTotal);
        statistics.setBackTotal(backTotal);

        statistics.setReleaseTotalPercentage(calculatePercentage(total, releaseTotal));
        statistics.setCheckTotalPercentage(calculatePercentage(total, checkTotal));
        statistics.setDraftTotalPercentage(calculatePercentage(total, draftTotal));
        statistics.setOfflineTotalPercentage(calculatePercentage(total, offlineTotal));
        statistics.setBackTotalPercentage(calculatePercentage(total, backTotal));
        return statistics;
    }


    /**
     * 计算百分比
     */
    private static String calculatePercentage(long total, long completeCount) {
        if (total == 0) {
            return "0.00";
        } else {
            double percent = ((double) completeCount / total) * 100;
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            return decimalFormat.format(percent);
        }
    }
}
